<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <meta name="referrer" content="no-referrer" />
</head>
<style>
    img {
        width: 60px;
    }
   
</style>

<body>
    <div class="container">
        <div id="app">
            <h2>电影Top250</h2>
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名字</th>
                        <th>海报</th>
                        <th>评分</th>
                        <th>是否收藏</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item of movies">
                        <td>{{item._id}}</td>
                        <td @click="handleToggle(item._id)">{{item.title}}</td>
                        <td><img :src="item.pic" alt=""></td>
                        <td>{{item.rating}}</td>
                        <td><img @click="handleCollect(item._id)"
                                :src="item.collected?'images/collect.png':'images/cancel.png'" alt="">
                        </td>
                        <td>
                            <button @click="handleDelete(item._id)" type="button" class="btn btn-danger">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <nav aria-label="Page navigation" class="pagination-lg text-center">
                <ul class="pagination">
                    <li>
                        <a href="javascript:void(0)" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li v-for="item of pages"><a @click="doPage(item)" href="javascript:void(0)">{{item}}</a></li>
                    <li>
                        <a href="javascript:void(0)" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                movies: [],
                total: 0
            },
            mounted() {
                $.ajax({
                    url: "http://localhost:8080/top250",
                    success: res => {
                        console.log(res);
                        this.movies = res.res;
                        this.total = res.total;
                    }
                })
            },
            methods: {
                async handleCollect(id) {
                    console.log(id);
                    var movies = this.movies;
                    var item = movies.find(item => {
                        return item._id == id
                    })
                    if (item.collected) {
                        item.collected = false;
                        await $.ajax({
                            url: "http://localhost:8080/collect/cancel",
                            data: {
                                id
                            },
                            type: "post"
                        })
                    } else {
                        item.collected = true;
                        await $.ajax({
                            url: "http://localhost:8080/collect",
                            data: {
                                id
                            },
                            type: "post"
                        })
                    }
                    this.movies = movies;
                },
                async doPage(num) {
                    var start = (num - 1) * 5;
                    console.log(start);
                    $.ajax({
                        url: `http://localhost:8080/top250?start=${start}`,
                        success: res => {
                            this.movies = res.res;
                            this.total = res.total;
                        }
                    })
                },
                async handleToggle(id) {
                    console.log(id);
                    location.href = `html/detail.html?id=${id}`
                },
                async handleDelete(id) {
                    // console.log(id);
                    var movies = this.movies.filter(item => {
                        return item._id != id
                    })
                    await $.ajax({
                        url: "http://localhost:8080/delete",
                        data: {
                            id
                        },
                        type: "post"
                    })
                    this.movies = movies;
                }
            },
            computed: {

                pages: function () {
                    var total = this.total;
                    var arr = [];
                    for (var i = 0; i < total; i += 5) {
                        arr.push(i);
                    }
                    var res = arr.map(item => {
                        return (item + 5) / 5
                    })
                    console.log(res);
                    return res;
                }

            }
        })
    </script>
</body>

</html>